package cn.iocoder.yudao.module.wms.service.commodity;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.wms.controller.admin.commodity.vo.CommodityPageReqVO;
import cn.iocoder.yudao.module.wms.controller.admin.commodity.vo.CommoditySaveReqVO;
import cn.iocoder.yudao.module.wms.dal.dataobject.commodity.CommodityDO;
import cn.iocoder.yudao.module.wms.dal.mysql.commodity.CommodityMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.UUID;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wms.enums.ErrorCodeConstants.*;


/**
 * 商品 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CommodityServiceImpl extends ServiceImpl<CommodityMapper, CommodityDO> implements CommodityService {

    @Resource
    private CommodityMapper commodityMapper;

    @Override
    public String createCommodity(CommoditySaveReqVO reqVO) {
        // 查询商品名称是否存在，存在则提示
        boolean exists = this.exists(Wrappers.lambdaQuery(CommodityDO.class)
                        .eq(CommodityDO::getName, reqVO.getName())
                        // 商品名有可能夹带英文，所以这边不去空格
                        // .eq(CommodityDO::getName, reqVO.getName().trim())
        );
        if (exists) {
            throw exception(COMMODITY_NAME_EXISTS);
        }
        // 插入
        CommodityDO commodityDO = BeanUtils.toBean(reqVO, CommodityDO.class);
        String now = LocalDateTimeUtil.formatNormal(LocalDateTime.now());
        commodityDO.setId(UUID.randomUUID().toString())
                .setCreateAt(now)
                .setUpdateAt(now);
        commodityMapper.insert(commodityDO);
        // 返回
        return commodityDO.getId();
    }

    @Override
    public void updateCommodity(CommoditySaveReqVO reqVO) {
        // 校验存在
        validateCommodityExists(reqVO.getId());
        // 查询商品名称是否存在，存在则提示
        boolean exists = this.exists(Wrappers.lambdaQuery(CommodityDO.class)
                        .ne(CommodityDO::getId, reqVO.getId())
                        .eq(CommodityDO::getName, reqVO.getName())
                        // 商品名有可能夹带英文，所以这边不去空格
                        // .eq(CommodityDO::getName, reqVO.getName().trim())
        );
        if (exists) {
            throw exception(COMMODITY_NAME_EXISTS);
        }
        // 更新
        CommodityDO updateObj = BeanUtils.toBean(reqVO, CommodityDO.class);
        updateObj.setUpdateAt(LocalDateTimeUtil.formatNormal(LocalDateTime.now()));
        commodityMapper.updateById(updateObj);
    }

    @Override
    public void updateCommodity(CommodityDO commodityDO) {
        commodityMapper.updateById(commodityDO);
    }


    @Override
    public void deleteCommodity(String id) {
        // 校验存在
        validateCommodityExists(id);
        // 删除
        commodityMapper.deleteById(id);
    }

    private void validateCommodityExists(String id) {
        if (commodityMapper.selectById(id) == null) {
            throw exception(COMMODITY_NOT_EXISTS);
        }
    }

    @Override
    public CommodityDO getCommodity(String id) {
        return commodityMapper.selectById(id);
    }

    @Override
    public PageResult<CommodityDO> getCommodityPage(CommodityPageReqVO pageReqVO) {
        return commodityMapper.selectPage(pageReqVO);
    }

}